﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Calais;
using System.Globalization;
using DataType;


namespace OpenCalaisConnector
{
    public class OpenCalaisconnector
    {
        private string apiKey = "sm9mxewfn2azafsjq8aqn6gf"; 

        public string DoCalaisRequest(string content)
        {
            // Pass in the content as a string and also your calais API key
            var calais = new CalaisDotNet(apiKey, content);

            // Tell calaisdotnet what output format you want to work with (ie CalaisSimpleDocument / CalaisRdfDocument / CalaisJsonDocument etc)
            var document = calais.Call<CalaisSimpleDocument>();

            // Output the raw response from the web service
            //Console.WriteLine(document.RawOutput);

            // Organize Entities in various ways (ie Relevance)
            var sorted = from item in document.Entities
                         orderby item.Relevance descending
                         select item;

            string result = "";

           NumberFormatInfo provider = new NumberFormatInfo();

            provider.NumberDecimalSeparator = ",";
            provider.NumberGroupSeparator = ".";
            provider.NumberGroupSizes = new int[] { 3 };

            foreach (var entity in sorted)
            {
                if (Convert.ToDouble(entity.Relevance, provider) > 0.6 && (entity.Type == CalaisSimpleEntityType.Company || entity.Type == CalaisSimpleEntityType.Person))
                {
                    result += "\"" + entity.Value + "\"|";
                }//Console.WriteLine(entity.Value + " / " + entity.Type + " / " + entity.Frequency + " / " + entity.Relevance);
            }
            return result; 
        }

        public List<TagEntry> DoCalaisRequestEntries(string content)
        {
            // Pass in the content as a string and also your calais API key
            var calais = new CalaisDotNet(apiKey, content);

            // Tell calaisdotnet what output format you want to work with (ie CalaisSimpleDocument / CalaisRdfDocument / CalaisJsonDocument etc)
            var document = calais.Call<CalaisSimpleDocument>();

            // Output the raw response from the web service
            //Console.WriteLine(document.RawOutput);

            // Organize Entities in various ways (ie Relevance)
            var sorted = from item in document.Entities
                         orderby item.Relevance descending
                         select item;

            List<TagEntry> result = new List<TagEntry>();

            NumberFormatInfo provider = new NumberFormatInfo();

            provider.NumberDecimalSeparator = ",";
            provider.NumberGroupSeparator = ".";
            provider.NumberGroupSizes = new int[] { 3 };

            foreach (var entity in sorted)
            {
                TagEntry en = new TagEntry();
                en.Value = entity.Value;
                en.Frequency = entity.Frequency;
                en.Relevance = entity.Relevance;
                en.Type = entity.Type.ToString();
                result.Add(en);
            }

            var sorted_topic = from item_topic in document.Topics
                               orderby item_topic.Taxonomy descending
                               select item_topic;

            foreach (var entity in sorted_topic)
            {
                TagEntry en = new TagEntry();
                en.Value = entity.Value;
                en.Relevance = entity.Score;
                en.Type = "Taxonomy";
                result.Add(en);
            }
            return result;
        }

    }
}
